<?php
Class Manager_Model_menu extends Zend_Db_Table
{	
    protected $_name ="menu";
	protected $_primary ="id";
	
   public function searchkq($keyword,$status=-1)
	{
		$db = Zend_Registry::get('connectDb');
			$query="SELECT menu . * ,language.title as ngonngu
                  	FROM menu INNER JOIN language ON menu.language_id=language.id
					WHERE (menu.published=$status or $status=-1) AND menu.title like '%$keyword%'
					ORDER BY menu.order desc";
			$result = $db->fetchAll($query)	;
		return $result;	
	}
	
   public  function listItemabout($status=-1,$countries=-1,$position=-1)
	{
		   $db = Zend_Registry::get('connectDb');
			$query="SELECT menu . * ,language.title as ngonngu
                  	FROM menu INNER JOIN language ON menu.language_id=language.id
                  	WHERE ((menu.published=$status or $status=-1) AND(menu.language_id=$countries or $countries=-1)AND (menu.position=$position or $position=-1))
					ORDER BY menu.id desc";
			$result = $db->fetchAll($query)	;
			return $result;	
	}
		
   public  function get_content_menu($id)
	{
		   $db = Zend_Registry::get('connectDb');
			$query="SELECT menu . * 
                  	FROM menu 
					WHERE (menu.id=$id)";
			$result = $db->fetchAll($query)	;
			return $result;	
	}
		
   public  function get_cat()
	{
		    $db = Zend_Registry::get('connectDb');
			$query="SELECT c . *
                  	FROM category as c 
					WHERE (c.published=1)
					ORDER BY c.id desc";
			$results = $db->fetchPairs($query);
			return $results;	
	}
	
   public  function news()
	{
		   $db = Zend_Registry::get('connectDb');
			$query="SELECT article . * 
                  	FROM article 
					WHERE article.published=1
					ORDER BY id desc";
			$result = $db->fetchPairs($query)	;
			return $result;	
	}
	
   public  function news_front($id)
	{
		   $db = Zend_Registry::get('connectDb');
			$query="SELECT news . * 
                  	FROM news 
					WHERE (news.published=1)and(news.cat_id=$id)
					ORDER BY news.id desc";
			$result = $db->fetchAll($query)	;
			return $result;	
	}
	
   public function save($data)
	{	
      $introtext = stripslashes($data['introtext']);
      if ($data['type']==2)
      {
         $cat=$data['category'];
      }
      elseif ($data['type']==3)
      {
        $cat=$data['category_product_id'];
      }
	 elseif ($data['type']==5)
      {
        $cat=$data['list_cat'];
      }
	 elseif ($data['type']==6)
      {
        $cat=$data['details_product'];
      }
	 elseif ($data['type']==7)
      {
        $cat=$data['solution'];
      }
      else 
         $cat=0;
      
	  if ($data['key']=='add')
		  {	
			$db = Zend_Registry::get('connectDb');
			$data1 = array(
			              	'title'         	   => $data['title'],
		                 	'parents'			   => $data['parents'],
		  	                'type'			       => $data['type'],
			                'position'			   => $data['position'],							
		  					'keyt_id'              => $cat,
		  	                'introtext'			   => $introtext,
		  					'published'	           => $data['published'],
		  					'order'	               => $data['order'],
			                'language_id'          => $data['language_id'],
		  				 );
	        $db->insert('menu', $data1);
		  }
	else 
		  {
		  $tblGroup = new Manager_Model_menu();
		  $where ='id ='.$data['id'];
    	   $data1 = array(
			               	'title'         	   => $data['title'],
    	                    'parents'			   => $data['parents'],
		  	                'type'			       => $data['type'],
			                'position'			   => $data['position'],							
		  					'keyt_id'              => $cat,
		  	                'introtext'			   => $introtext,
		  					'published'	           => $data['published'],
		  					'order'	               => $data['order'],
			                'language_id'          => $data['language_id'],
		  				 );
    	  $tblGroup->update($data1,$where);
		  
		  }  
     }

   public function delete_data($id)
	{
		  	$tblGroup = new Manager_Model_menu();
			$where = "id =$id";
		    $tblGroup->delete($where);  	
	}

   public function delete_muti($arrParam = null)
	{
	$tblGroup = new Manager_Model_menu();
		if(count($arrParam['cid'])>0)
	 	{
			$ids = implode(',', $arrParam['cid']);
			$where = 'id IN (' . $ids . ')'; 
			$tblGroup->delete($where); 
			 
	 	}
	}
	
    public  function get_section()
	{
		    $db = Zend_Registry::get('connectDb');
			$query="SELECT s .id,s.title
                  	FROM category_product as s 
					WHERE (s.published=1)
					ORDER BY s.id desc";
			$results = $db->fetchPairs($query);
			return $results;	
	}
	
    public  function get_language()
	{
		    $db = Zend_Registry::get('connectDb');
			$query="SELECT l .id,l.title
                  	FROM language as l 
					WHERE (l.published=1)
					ORDER BY l.id desc";
			$results = $db->fetchPairs($query);
			return $results;	
	}
	
    public  function get_about()
	{
		    $db = Zend_Registry::get('connectDb');
			$query="SELECT s .id,s.title
                  	FROM article as s 
					WHERE (s.published=1)
					ORDER BY s.id desc";
			$results = $db->fetchPairs($query);
			return $results;	
	}
	
   public  function get_list_cat()
	{
		    $db = Zend_Registry::get('connectDb');
			$query="SELECT s .id,s.title
                  	FROM category as s 
					WHERE (s.published=1)
					ORDER BY s.id desc";
			$results = $db->fetchPairs($query);
			return $results;	
	}
	
   public  function get_details_product()
	{
		    $db = Zend_Registry::get('connectDb');
			$query="SELECT s .id,s.title
                  	FROM details_product as s 
					WHERE (s.published=1)
					ORDER BY s.id desc";
			$results = $db->fetchPairs($query);
			return $results;	
	}
	
   public  function list_news()
	{
		    $db = Zend_Registry::get('connectDb');
			$query="SELECT l.id,l.title
                  	FROM language as l
					WHERE (l.published=1)
					ORDER BY l.id desc";
			$results = $db->fetchPairs($query);
			return $results;	
	}
	
    public  function get_solution()
	{
		    $db = Zend_Registry::get('connectDb');
			$query="SELECT ss .id,ss.title
                  	FROM category_solution as ss
					WHERE (ss.published=1)
					ORDER BY ss.id desc";
			$results = $db->fetchPairs($query);
			return $results;	
	}
	
    public  function get_menu_parent()
	{
		   $db = Zend_Registry::get('connectDb');
			$query="SELECT menu .id,title,parents 
                  	FROM menu 
					WHERE published=1";
			$result = $db->fetchAll($query)	;
			return $result;	
	}
	
    public function get_content($menu,$parents = 0,$level = 1,&$newMenu)
	{
		foreach ($menu as $key => $val){
			if($val['parents'] == $parents){
				$val['level'] = $level;
				$newMenu[] = $val;
				unset($menu[$key]);
				$this->get_content($menu,$val['id'],$val['level'] + 1,$newMenu);
			}
		}
		
		return $newMenu;
	}
	
   
}